package org.example.io;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSort {
    public static void main(String[] args) throws Exception{

        //创建流对象
        FileReader reader = new FileReader("D:\\tmp\\sort.txt");
        char[] chars = new char[1024];
        int len;
        StringBuilder builder = new StringBuilder();
        while ((len = reader.read(chars))!=-1){
            builder.append(new String(chars,0,len));
        }
        String str = builder.toString();
        //将文本转换为数组
        String[] split = str.split("-");
        //创建list集合
        List<Integer> list = new ArrayList<>();
        for (String string : split) {
            list.add(Integer.parseInt(string));
        }
        //对集合进行排序
        Collections.sort(list);
        //关闭流对象
        reader.close();

        //创建流对象
        FileWriter writer = new FileWriter("D:\\tmp\\sort.txt");
        //写入数据
        for (int i = 0; i < list.size(); i++) {
            if(i == list.size() - 1){
                writer.write(list.get(i) + "");
            }else{
                writer.write(list.get(i) + "-");
            }
        }
        //关闭流对象
        writer.close();
    }
}
